<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="/static/css/index.css" rel="stylesheet">
<title>设备管理</title>
</head>
<body>

<div class="inner-title">
	<h3 class="fn f14"><img src="/static/images/icon-1.png" alt="" /> 设备管理</h3>
</div>

<div class="inner-cont">
	<div class="bgf2 creat-wrap" onclick="add();">
		<button class="creat-btn cf"><img src="/static/images/icon-add.png" alt="" /> 新增设备</button>
	</div>
	
	<div class="mt30">
		<table border="0"class="table">
			<tr>
				<th width="74">序号</th>
				<th width="500">设备名称</th>
				<th width="500">设备具体位置</th>
				<th width="500">激活时间</th>
				<th width="300">设备状态</th>
				<th width="500">最后心跳时间</th>
				<th width="500">产品序列号</th>
				<th width="200">操作</th>
			</tr>
			
			<tbody id="table">
			</tbody>
		</table>
		<div align="center" style="font-size: 18px; color: gray" id="page">

		</div>
	</div>
	
</div>


<!-- 弹窗 -->
<div class="pop pa bgf5 hide">
	<span class="pa pop-shut"></span>
	<h3 class="cf pop-title" id="h3">添加设备</h3>
	<div class="pop-cont">
		<form action="/web/addEquipments" method="post" class="form form-2 f14" id="form">
			<table class="dialog-table">
		        <tr id="esn">
		            <td>设备SN码</td>
		            <td><input type="text" id="sN" name="sN"/></td>
		            <td></td>
		        </tr>
		        <tr>
		            <td>设备名称</td>
		            <td>
		            	<input type="text" id="eName" name="eName"/>
		            	<input type="hidden" id="eId" name="eId" />
		            </td>
		            <td></td>
		        </tr>
		        <tr>
		            <td>设备具体位置</td>
		            <td><input type="text" id="eLocation" name="eLocation"/></td>
		        </tr>
		        
		        <tr>
		            <td></td>
		            <td><button id="btn_modifyOrCreate">提交</button></td>
		            <td></td>
		        </tr>
		    </table>
			 <div align="center" style="font-size: 18px;color: gray" id="page" >
				
			</div>
		</form>
	</div>
</div>




<script src="/static/js/jquery.js" type="text/javascript"></script>
<script src="https://unpkg.com/sweetalert/dist/sweetalert.min.js"></script>

<script type="text/javascript">
			var count=0;
			$(function(){
				$.ajax({
					url:"/web/getPageEquipments/[[${session.user.cId}]]/1",
					success:function(response){
					if(response.success){
						$.each(response.data,function(i,eq){
							if(eq.eStatus!=2){
								var eStatus ;
								if(eq.eStatus==0){
									eStatus="离线";
								}else if(eq.eStatus==1){
									eStatus="在线";
								}
								var finalHeartbeatTime;
								if(eq.finalHeartbeatTime==null||eq.finalHeartbeatTime==""){
									finalHeartbeatTime="--"
								}else{
									finalHeartbeatTime=eq.finalHeartbeatTime
								}
								count++;
								$("#table").append('<tr ><td >'+count+'</td><td >'+eq.eName+'</td><td>'+eq.eLocation+'</td><td>'+eq.activationTime+'</td><td >'+eStatus+'</td><td >'+finalHeartbeatTime+'</td><td>'+eq.sN+'</td><td ><img src="/static/images/icon-edit.png" title="编辑" alt="" style="cursor:pointer;" onclick="upd('+eq.eId+',this);" /><img src="/static/images/icon-delete.png" title="删除"  onclick="del('+eq.eId+',this);" style="cursor:pointer;"/></td></tr>');
							
							}
						});
						if(response.pagenum>1){
							for(var i=1;i<=response.pagenum;i++){
								$("#page").append('<button type="button" style="margin-left: 30px" onclick="page(this);" value="'+i+'">'+i+'</button>');
							}
						}
					}
				},error:function(){
					alert("出现未知错误,请联系站内管理员!");
				}
					
				});
				// 展示弹窗
			
				
				// 关闭弹窗
				$('.pop-shut').on('click', function(e) {
					$(this).parent().hide();
				})
				
				
			});
			function add() {
				$("#h3").text("新增设备");
				$('.pop').show();
			}
			function page(event){
				var a = $(event).val();
				if(a==1){
					count=0;
				}else{
					count=(a-1)*20
				}
				$.ajax({
					url:"/web/getPageEquipments/[[${session.user.cId}]]/"+a,
					success:function(response){
						if(response.success){
							$("#table").empty();
							$.each(response.data,function(i,eq){
								if(eq.eStatus!=2){
									var eStatus ;
									if(eq.eStatus==0){
										eStatus="离线";
									}else if(eq.eStatus==1){
										eStatus="在线";
									}
									var finalHeartbeatTime;
									if(eq.finalHeartbeatTime==null||eq.finalHeartbeatTime==""){
										finalHeartbeatTime="--"
									}else{
										finalHeartbeatTime=eq.finalHeartbeatTime
									}
									count++;
									$("#table").append('<tr id="'+eq.eId+'" ><td >'+count+'</td><td >'+eq.eName+'</td><td>'+eq.eLocation+'</td><td>'+eq.activationTime+'</td><td >'+eStatus+'</td><td >'+finalHeartbeatTime+'</td><td>'+eq.sN+'</td><td ><a  class="mr20 editPop" href="/web/toupdEquipments/'+eq.eId+'"><img src="/static/images/icon-edit.png" title="编辑" style="cursor:pointer;"/></a><img src="/static/images/icon-delete.png" title="删除"  onclick="del('+eq.eId+',this);" style="cursor:pointer;"/></td></tr>');
									
								}
							});
						}
					},error:function(){
						alert("出现未知错误,请联系站内管理员!");
					}
				});
			}
			function del(eid,event) {
				swal({
					  title: "确认删除",
					  text: "确认删除？",
					  icon: "warning",
					  buttons: ["取消", "确认"],
					  dangerMode: true,
					})
					.then((willDelete) => {
						$.ajax({
							url:"/web/delEquipments/"+eid,
							success:function(response){
								if(response.success){
									$(event).parent().parent().remove();
								}
							},error:function(){
								swal({
									  title: "删除失败",
									  text: "删除失败",
									  icon: "error",
									  buttons: [false, "确认"],
									  dangerMode: true
									});
							}
						});
					});
				
			}
			function upd(eid,event) {
				$.ajax({
					url:"/web/toupdEquipments/"+eid,
					success:function(response){
						if(response.success){
							var eq=response.data;
							$("#h3").text("修改设备信息");
							$("#sN").val(eq.sN);
							$("#sN").attr("disabled","disabled");
							$("#eName").val(eq.eName);
							$("#eLocation").val(eq.eLocation);
							$("#eId").val(eq.eId);
							$("#form").attr("action","/web/doupdEquipments");
							$('.pop').show();
						}
					},error:function(){
						
					}
				});
			}
			$.fn.validate = function(tips){
				var is=false;
			    if($(this).val() == "" || $.trim($(this).val()).length == 0){
			        alert(tips+"不能为空！");
			    }else{
			    	is=true;
			    }
			    return is;
			}
			$('#form').submit(function() {
				var result=false;
		    	if($("#sN").validate("设备S/N码")==true&&$("#eName").validate("设备名称")==true&&$("#eLocation").validate("设备具体位置")==true){
					result=true;
				}
				if(result==false){
					return false;
				}
		 
		});
</script>
</body>
</html>